package com.tanhua.dubbo.api;

import com.mongodb.client.result.DeleteResult;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class CommentsApiImpl implements CommentsApi {

    @Autowired
    private MongoTemplate mongoTemplate;
    //查询评论
    public List<Comment> comments(String movementId, Integer commentType, Integer page, Integer pagesize) {
        //      根据id   和  类型 查询
        Criteria criteria = Criteria.where("publishId").is(new ObjectId(movementId)).and("commentType").is(commentType);
        Query query = Query.query(criteria).with(Sort.by(Sort.Order.desc("created"))).skip((page - 1) * pagesize).limit(pagesize);
        List<Comment> list = mongoTemplate.find(query, Comment.class);

        return list;
    }

    //保存喜欢评论点赞
    public Integer saveComments(Comment comments) {
        //查询动态对象
        ObjectId movementId = comments.getPublishId();
        Movement movement = mongoTemplate.findById(movementId, Movement.class);
        //设置动态发布人id
        comments.setPublishUserId(movement.getUserId());
        //保存
        mongoTemplate.save(comments);
        //更新动态
        Criteria criteria = Criteria.where("id").is(movementId);
        Query query = Query.query(criteria);
        Integer commentType = comments.getCommentType();
        //设置更新字段
        Update update = new Update();
        if (commentType == 1){
            update.inc("likeCount",1);
        }else if (commentType ==2){

            update.inc("commentCount",1);
        }else {
            update.inc("loveCount",1);
        }
        //更新
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        Movement modify = mongoTemplate.findAndModify(query, update, options, Movement.class);
        //返回
        if(commentType == 1) {
            return modify.getLikeCount();
        }else if (commentType ==2) {
            return modify.getCommentCount();
        }else{
            return modify.getLoveCount();
        }
    }

    //删除
    public Integer deleteComments(String movementId, Long userId, Integer commentType) {
        //删除
        Criteria criteria = Criteria.where("userId").is(userId).and("publishId").is(new ObjectId(movementId)).and("commentType").is(commentType);
        Query query = Query.query(criteria);
        mongoTemplate.remove(query, Comment.class);
        //更新动态
        Criteria mCriteria = Criteria.where("id").is(movementId);
        Query mQuery = Query.query(mCriteria);

        //设置更新字段
        Update update = new Update();
        if (commentType == 1){
            update.inc("likeCount",-1);
        }else if (commentType ==2){

            update.inc("commentCount",-1);
        }else {
            update.inc("loveCount",-1);
        }
        //更新
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        Movement modify = mongoTemplate.findAndModify(mQuery, update, options, Movement.class);
        //返回
        if(commentType == 1) {
            return modify.getLikeCount();
        }else if (commentType ==2) {
            return modify.getCommentCount();
        }else{
            return modify.getLoveCount();
        }
    }

   //评论点赞
    public Integer addLikeCount(String id) {

        Criteria criteria = Criteria.where("id").is(id);
        Query query = Query.query(criteria);
        //设置更新字段
        Update update = new Update();
        update.inc("likeCount",1);
        //更新
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        Comment modify = mongoTemplate.findAndModify(query, update, options, Comment.class);
        return modify.getLikeCount();
    }

   //取消评论点赞
    public Integer deleteLikeCount(String id) {
        Criteria criteria = Criteria.where("id").is(id);
        Query query = Query.query(criteria);
        //设置更新字段
        Update update = new Update();
        update.inc("likeCount",-1);
        //更新
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        Comment modify = mongoTemplate.findAndModify(query, update, options, Comment.class);
        return modify.getLikeCount();
    }

    //根据动态id查询评论
    public PageResult findCommentsById(String id, Integer page, Integer pagesize) {
        Criteria criteria = Criteria.where("publishId").is(new ObjectId(id)).and("commentType").is(2);
        Query query = Query.query(criteria);
        long count = mongoTemplate.count(query, Comment.class);
        query.with(Sort.by(Sort.Order.desc("created")))
                .skip((page - 1) * pagesize)
                .limit(pagesize);
        List<Comment> list = mongoTemplate.find(query, Comment.class);

        return new PageResult(page,pagesize,count,list);
    }
}
